package com.seanchenxi.gwt.wordpress.xmlrpc.service.impl;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.seanchenxi.gwt.wordpress.domain.model.PostList;
import com.seanchenxi.gwt.wordpress.domain.model.SimpleCategory;
import com.seanchenxi.gwt.wordpress.domain.model.TrackBackPing;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.MovableTypeServiceAsync;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.constant.HasMovableTypeServiceMethodNames;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.CategoriesResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.PostListsResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.TrackBackPingsResponseHandler;
import com.seanchenxi.gwt.xmlrpc.client.XmlRpcClient;
import com.seanchenxi.gwt.xmlrpc.client.XmlRpcRequest;

/**
 * The Class MovableTypeServiceAsyncImpl.
 */
public class MovableTypeServiceAsyncImpl extends XmlRpcClient implements
		MovableTypeServiceAsync, HasMovableTypeServiceMethodNames {

	/**
	 * Instantiates a new movable type service async impl.
	 *
	 * @param url the xmlrpc url
	 */
	public MovableTypeServiceAsyncImpl(String url) {
		super(url);
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MovableTypeServiceAsync#getCategoryList(int, java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getCategoryList(int blogId, String username, String password,
			AsyncCallback<ArrayList<SimpleCategory>> callback) {
		XmlRpcRequest<ArrayList<SimpleCategory>> xmlRpcRequest = createRequest(
				GET_POST_CATEGORIES, blogId, username, password);
		send(xmlRpcRequest, callback,
				new CategoriesResponseHandler<SimpleCategory>());
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MovableTypeServiceAsync#getRecentPostTitles(int, java.lang.String, java.lang.String, int, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getRecentPostTitles(int blogId, String username,
			String password, int number,
			AsyncCallback<ArrayList<PostList>> callback) {
		XmlRpcRequest<ArrayList<PostList>> xmlRpcRequest = createRequest(
				GET_RECENT_POST_TITLES, blogId, username, password, number);
		send(xmlRpcRequest, callback, new PostListsResponseHandler());
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MovableTypeServiceAsync#getPostCategories(int, java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getPostCategories(int postId, String username, String password,
			AsyncCallback<ArrayList<SimpleCategory>> callback) {
		XmlRpcRequest<ArrayList<SimpleCategory>> xmlRpcRequest = createRequest(
				GET_POST_CATEGORIES, postId, username, password);
		send(xmlRpcRequest, callback,
				new CategoriesResponseHandler<SimpleCategory>());
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MovableTypeServiceAsync#setPostCategories(int, java.lang.String, java.lang.String, java.util.ArrayList, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void setPostCategories(int postId, String username, String password,
			ArrayList<SimpleCategory> categories,
			AsyncCallback<Boolean> callback) {
		XmlRpcRequest<Boolean> xmlRpcRequest = createRequest(
				SET_POST_CATEGOTIES, postId, username, password, categories);
		send(xmlRpcRequest, callback);
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MovableTypeServiceAsync#supportedMethods(com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void supportedMethods(AsyncCallback<Object> callback) {
		XmlRpcRequest<Object> xmlRpcRequest = createRequest(SUPPORTED_METHODS);
		send(xmlRpcRequest, callback);
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MovableTypeServiceAsync#getTrackbackPings(int, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getTrackbackPings(int postId,
			AsyncCallback<ArrayList<TrackBackPing>> callback) {
		XmlRpcRequest<ArrayList<TrackBackPing>> xmlRpcRequest = createRequest(
				GET_TRACKBACK_PINGS, postId);
		send(xmlRpcRequest, callback, new TrackBackPingsResponseHandler());
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MovableTypeServiceAsync#publishPost(int, java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void publishPost(int postId, String username, String password,
			AsyncCallback<Integer> callback) {
		XmlRpcRequest<Integer> xmlRpcRequest = createRequest(PUBLISH_POST,
				postId, username, password);
		send(xmlRpcRequest, callback);
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MovableTypeServiceAsync#supportedTextFilters()
	 */
	@Override
	public void supportedTextFilters() {
		// We do nothing
	}

}
